package com.skynet.app.api.auth.handler;

import com.skynet.app.api.auth.exception.UserAuthException;
import com.skynet.app.api.auth.util.ResultVOUtil;
import com.skynet.app.api.auth.vo.DataVO;
import com.skynet.app.api.auth.vo.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import static com.skynet.app.api.auth.enums.ResultEnum.UNKNOWN_ERROR;


@ControllerAdvice
@Slf4j
public class UserAuthExceptionHandler {

    @ExceptionHandler(UserAuthException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ResponseBody
    public ResultVO handleUserAuthException(UserAuthException userAuthException) {
        DataVO dataVO = new DataVO();
        dataVO.setErrors(userAuthException.getMessage());
        return ResultVOUtil.error(dataVO);
    }

    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.BAD_GATEWAY)
    @ResponseBody
    public ResultVO handleException(Exception exception) {
        log.error("【系统异常】异常消息：", exception);
        return ResultVOUtil.error(UNKNOWN_ERROR);
    }


}
